<template>
  <div>
    <BasicTable @register="registerTable">
      <template #headerTop>
        <Alert type="info" show-icon>
          <template #message> 修改菜单可能造成风险！请谨慎操作！ </template>
        </Alert>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增菜单 </a-button>
        <div>
          <Upload
            :multiple="false"
            name="file"
            :customRequest="beforeUpload"
            :show-upload-list="false"
            class="upload-modal-toolbar__btn"
            accept=".json"
          >
            <a-button type="primary"> 菜单导入 </a-button>
          </Upload>
        </div>
        <a-button type="primary" @click="expandAll">展开全部</a-button>
        <a-button type="primary" @click="collapseAll">折叠全部</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  confirm: handleDelete.bind(null, record),
                },
              },
              {
                icon: 'ant-design:copy-outlined',
                onClick: copyMenu.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <MenuDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="Ba_Menu">
  import { Upload, Alert } from 'ant-design-vue';
  import { toRaw } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getMenuList, deleteMenu, batchAddMenu } from '/@/api/work/system/menu';
  import { IMenu, genMenuMap } from '/@/jv_package/libs/genMenus/core/genTree';
  import { useCopyToClipboard } from '/@/hooks/web/useCopyToClipboard';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { omit } from 'lodash-es';
  import { useDrawer } from '/@/components/Drawer';
  import MenuDrawer from './MenuDrawer.vue';

  import { columns, searchFormSchema } from './menu.data';
  const { clipboardRef } = useCopyToClipboard();
  const { createMessage } = useMessage();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, expandAll, collapseAll }] = useTable({
    title: '菜单列表',
    api: getMenuList,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    rowKey: 'id',
    isTreeTable: true,
    pagination: false,
    striped: false,
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    defaultExpandAllRows: false,
    canResize: false,
    actionColumn: {
      width: 160,
      title: 'Action',
      dataIndex: 'action',
      fixed: 'right',
    },
  });

  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete(record: Recordable) {
    // console.log(record);
    await deleteMenu([record.id]);
    reload();
  }

  function handleSuccess() {
    reload();
  }

  function beforeUpload(file) {
    const reader = new FileReader();
    reader.readAsText(file.file);
    reader.onload = async () => {
      const menus: IMenu[] = [JSON.parse(reader.result as string)];
      const menuLIst = genMenuMap({ menuList: menus });
      await batchAddMenu(menuLIst);
      reload();
    };
  }
  function copyMenu(record: Recordable) {
    let res = omit(toRaw(record), ['children', 'id']);
    clipboardRef.value = JSON.stringify(res);
    createMessage.success(`复制成功`);
  }
</script>
